# Maintainer: Evangelos Foutras <evangelos@foutrelis.com>

pkgname=('geoip-database' 'geoip-database-extra')
pkgver=20230901
pkgrel=1
arch=('any')
url="https://mailfud.org/geoip-legacy/"
license=('CCPL:by-sa')
makedepends=('util-linux')
checkdepends=('geoip')
_source_base=https://sources.archlinux.org/other/packages/$pkgname/$pkgver
source=(GeoIP-$pkgver.dat.gz::$_source_base/GeoIP.dat.gz
        GeoIPv6-$pkgver.dat.gz::$_source_base/GeoIPv6.dat.gz
        GeoIPCity-$pkgver.dat.gz::$_source_base/GeoIPCity.dat.gz
        GeoIPCityv6-$pkgver.dat.gz::$_source_base/GeoIPCityv6.dat.gz
        GeoIPASNum-$pkgver.dat.gz::$_source_base/GeoIPASNum.dat.gz
        GeoIPASNumv6-$pkgver.dat.gz::$_source_base/GeoIPASNumv6.dat.gz)
sha256sums=('3cc915a2a37e5fec0a11850dcf0c3f531ac11bd83dee99c8aa3946a229fc7732'
            '16c14488bbc06b599be4342588a2ff949bda002b66b1b3011f6e2d2bbb01ed73'
            '3fe1e42b1992060165c79168e6a18cf7ccfed26e5cba641052435eb68f1db32b'
            '21ab998910417ccf8a4552d91ee2e8034e2fab52fa8b723d3c78901869ed092a'
            'cc40c97892e87201b4c4349f2175d6f6dc8834b10a3af7247a1b93ab0177e902'
            '85979f42a3115cc466afe4346b1f933b76f295843459e9452de176b8f383e7e2')

prepare() {
  cd "$srcdir"
  rename -v -- "-$pkgver" '' *.dat
}

check() {
  cd "$srcdir"

  if [[ $(geoiplookup -f GeoIP.dat archlinux.org) != *'FI, Finland' ]]; then
    echo >&2 'Unable to resolve IPv4 address to country.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPv6.dat archlinux.org) != *'FI, Finland' ]]; then
    echo >&2 'Unable to resolve IPv6 address to country.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPCity.dat archlinux.org) != *'FI, 18, N/A, Helsinki'* ]]; then
    echo >&2 'Unable to resolve IPv4 address to city.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPCityv6.dat archlinux.org) != *'FI, 18, Helsinki'* ]]; then
    echo >&2 'Unable to resolve IPv6 address to city.'
    return 1
  fi

  if [[ $(geoiplookup -f GeoIPASNum.dat archlinux.org) != *'AS24940 Hetzner Online GmbH' ]]; then
    echo >&2 'Unable to resolve IPv4 address to ASN.'
    return 1
  fi

  if [[ $(geoiplookup6 -f GeoIPASNumv6.dat archlinux.org) != *'AS24940 Hetzner Online GmbH' ]]; then
    echo >&2 'Unable to resolve IPv6 address to ASN.'
    return 1
  fi
}

package_geoip-database() {
  pkgdesc="GeoIP legacy country database (based on GeoLite2 data created by MaxMind)"

  cd "$srcdir"

  install -d "$pkgdir/usr/share/GeoIP"
  install -m644 -t "$pkgdir/usr/share/GeoIP" GeoIP{,v6}.dat
}

package_geoip-database-extra() {
  pkgdesc="GeoIP legacy city/ASN databases (based on GeoLite2 data created by MaxMind)"

  cd "$srcdir"

  install -d "$pkgdir/usr/share/GeoIP"
  install -m644 -t "$pkgdir/usr/share/GeoIP" GeoIPCity{,v6}.dat GeoIPASNum{,v6}.dat
}

# vim:set ts=2 sw=2 et:
